﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QW.Core
{
    /// <summary>
    /// 通用数据返回动态模型
    /// </summary>
    public class CommonResult : System.Dynamic.DynamicObject
    {
        #region 字段
        private Dictionary<string, object> _members;
        #endregion

        #region 构造函数
        /// <summary>
        /// .ctor
        /// </summary>
        public CommonResult()
        {
            _members = new Dictionary<string, object>();
            //给初始值
            success = false;
            total = 0;
            code = -1;
            msg = "";
        }
        #endregion

        #region 属性
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool success
        {
            get
            {
                var value = _members["success"];
                if (value != null)
                    return (bool)value;
                return false;
            }
            set
            {
                _members["success"] = value;
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        public string msg
        {
            get
            {
                var value = _members["msg"];
                if (value != null)
                    return (string)value;
                return string.Empty;
            }
            set
            {
                _members["msg"] = value;
            }
        }

        /// <summary>
        /// 状态
        /// <para>1表成功，0表示未处理（默认）</para>
        /// </summary>
        public int code
        {
            get
            {
                var value = _members["code"];
                if (value != null)
                    return (int)value;
                return 0;
            }
            set
            {
                _members["code"] = value;
            }
        }
        /// <summary>
        /// 数据总量
        /// </summary>
        public long total
        {
            get
            {
                var value = _members["total"];
                if (value != null)
                    return (long)value;
                return 0;
            }
            set
            {
                _members["total"] = value;
            }
        }
        #endregion

        #region 重写方法
        /// <summary>
        /// 尝试给值
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool TrySetMember(System.Dynamic.SetMemberBinder binder, object value)
        {
            if (!_members.ContainsKey(binder.Name))
            {
                _members.Add(binder.Name, value);
                return true;
            }

            return base.TrySetMember(binder, value);
        }
        /// <summary>
        /// 尝试取值
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
        {
            if (_members.ContainsKey(binder.Name))
            {
                result = _members[binder.Name];
                return true;
            }

            return base.TryGetMember(binder, out result);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return base.GetDynamicMemberNames().Concat(_members.Keys);
        }
        #endregion

        #region 快捷返回
        /// <summary>
        /// 结果
        /// </summary>
        /// <param name="success"></param>
        /// <param name="msg"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static CommonResult Result(bool success, string msg, int code)
        {
            dynamic result = new CommonResult
            {
                success = success,
                msg = msg,
                code = code,
                total=0
            };
            return result;
        }
        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static CommonResult SuccessResult(string msg = null, int code = (int)CommonResultCode.success)
        {
            dynamic result = new CommonResult
            {
                success = true,
                msg = msg,
                code = code,
                total = 0
            };
            return result;
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static CommonResult FailResult(string msg = null, int code = (int)CommonResultCode.fail)
        {
            dynamic result = new CommonResult
            {
                success = false,
                msg = msg,
                code = code,
                total = 0
            };
            return result;
        }
        #endregion
    }
    /// <summary>
    /// 通用返回状态码
    /// </summary>
    public enum CommonResultCode
    {
        /// <summary>
        /// 失败
        /// </summary>
        fail = -1,
        /// <summary>
        /// 成功
        /// </summary>
        success = 0,
    }
}
